﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Formality.Parsing.ContextFree.LR
{
    public class Item
    {
        public Item(int Position, Production Production, Terminal Lookahead)
        {
            this.Position = Position;
            this.Production = Production;
            this.Lookahead = Lookahead;

            Hash = Position ^ Production.GetHashCode();

            if (Lookahead != null)
                Hash ^= Lookahead.GetHashCode();
        }

        public int Position { get; private set; }
        public Production Production { get; private set; }
        public Terminal Lookahead { get; private set; }

        public bool ShouldReduce(Terminal Lookahead)
        {
            return this.Lookahead == Lookahead
                && Position == Production.Symbols.Length;
        }

        public bool ShouldShift(Terminal Terminal)
        {
            return Position < Production.Symbols.Length
                && Terminal == Production.Symbols[Position];
        }

        public INonterminal NonterminalAtPosition
        {
            get
            {
                if (Position < Production.Symbols.Length)
                    return Production.Symbols[Position] as INonterminal;
                return null;
            }
        }

        public ISymbol[] SymbolsAfterPosition
        {
            get
            {
                var Len = Production.Symbols.Length - Position - 1;

                ISymbol[] BetaA;

                if (Lookahead != null)
                {
                    BetaA = new ISymbol[Len + 1];
                    BetaA[Len] = Lookahead;
                }
                else
                {
                    BetaA = new ISymbol[Len];
                }

                for (var i = 0; i < Len; i++)
                    BetaA[i] = Production.Symbols[i + Position + 1];

                return BetaA;
            }
        }

        public override string ToString()
        {
            return string.Format("[{0} -> {1}.{2}, {3}]",
                Production.Head,
                Production.Symbols.Take(Position).Join(" "),
                Production.Symbols.Skip(Position).Join(" "),
                Lookahead == null ? "$" : Lookahead.ToString());
        }

        public override bool Equals(object obj)
        {
            var i = obj as Item;

            return i == null
                ? false
                : i.Position == Position
                    && i.Production == Production
                    && i.Lookahead == Lookahead;
        }

        public override int GetHashCode() { return Hash; }

        public Item Duplicate()
        {
            return new Item(Position, new Production
                {
                    Head = Production.Head,
                    SemanticFunc = Production.SemanticFunc,
                    Symbols = Production.Symbols
                }, Lookahead);
        }

        private int Hash;
    }
}
